package com.github.fujianlian.klinechart.draw;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.github.fujianlian.klinechart.BaseKLineChartView;
import com.github.fujianlian.klinechart.indicator.DataHelper;
import com.github.fujianlian.klinechart.R;
import com.github.fujianlian.klinechart.base.IValueFormatter;
import com.github.fujianlian.klinechart.entity.h.ISKDJ;
import com.github.fujianlian.klinechart.formatter.ValueFormatter;

import java.util.List;


/**
 * KDJ实现类
 * Created by tifezh on 2016/6/19.
 */

public class SKDJDraw extends BaseDraw<ISKDJ,ISKDJ> {

    private Paint mKPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mDPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    float lineTop = 85;
    float lineBottom = 20;
    public SKDJDraw() {
        super(ISKDJ.class);

    }

    public SKDJDraw(BaseKLineChartView view, float lineTop, float lineBottom) {
        this(view);
        this.lineTop = lineTop;
        this.lineBottom = lineBottom;
    }

    public SKDJDraw(BaseKLineChartView view) {
        super(ISKDJ.class);

        init(view);
    }


    @Override
    public void init(BaseKLineChartView view) {
        mKPaint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_red));
        mDPaint.setColor(ContextCompat.getColor(view.getContext(), R.color.chart_green));


        mKPaint.setStrokeWidth(2);
        mDPaint.setStrokeWidth(2);
    }

    public float getLineTop() {
        return lineTop;
    }

    public void setLineTop(float lineTop) {
        this.lineTop = lineTop;
    }

    public float getLineBottom() {
        return lineBottom;
    }

    public void setLineBottom(float lineBottom) {
        this.lineBottom = lineBottom;
    }


    @Override
    public void drawTranslated(@Nullable ISKDJ lastPoint, @NonNull ISKDJ curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position) {
//
//        if (!lastPoint.getSk().isNaN()) {
//            view.drawVolLine(this, canvas, mDPaint, lastX, lastPoint.getSk(), curX, curPoint.getSk());
//        }
//        if (!lastPoint.getSk().isNaN()) {
//            view.drawVolLine(this, canvas, mKPaint, lastX, lastPoint.getSd(), curX, curPoint.getSd());
//        }
    }

    @Override
    public void drawText(@NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position, float x, float y) {
        ISKDJ point = (ISKDJ) view.getItem(position);
        String text = "SKDJ(" + DataHelper.getSkdj_M() + "," + DataHelper.getSkdj_N() + ")  ";
        canvas.drawText(text, x, y, view.getTextPaint());
        x += view.getTextPaint().measureText(text);
        text = "K:" + view.formatValue(point.getSk()) + " ";
        canvas.drawText(text, x, y, mKPaint);
        x += mKPaint.measureText(text);
        text = "D:" + view.formatValue(point.getSd()) + " ";
        canvas.drawText(text, x, y, mDPaint);
    }

    @Override
    public Float getMaxValue(ISKDJ point) {
        return Math.max(point.getSk(), point.getSd());
    }

    @Override
    public float getMinValue(ISKDJ point) {
        return Math.min(point.getSk(), point.getSd());
    }

    @Override
    public IValueFormatter getValueFormatter() {
        return new ValueFormatter();
    }

    @Override
    public void drawTranslateds(Canvas canvas, BaseKLineChartView view, List<ISKDJ> datas, int mStartIndex, int mStopIndex) {


        /**
         * 绘制kd
         */
        float[] pts = new float[(mStopIndex - mStartIndex + 1) * 4];
        float[] pts1 = new float[(mStopIndex - mStartIndex + 1) * 4];

        for (int i = mStartIndex; i <= mStopIndex; i++) {
            ISKDJ currentPoint = (ISKDJ) view.getItem(i);

            float currentPointX = view.getX(i);

            ISKDJ lastPoint = i == 0 ? currentPoint : (ISKDJ) view.getItem(i - 1);
            float lastX = i == 0 ? currentPointX : view.getX(i - 1);

            pts[(i - mStartIndex) * 4] = lastX;
            pts[(i - mStartIndex) * 4 + 1] = view.getVolY(this, lastPoint.getSk());
            pts[(i - mStartIndex) * 4 + 2] = currentPointX;
            pts[(i - mStartIndex) * 4 + 3] = view.getVolY(this, currentPoint.getSk());

            pts1[(i - mStartIndex) * 4] = lastX;
            pts1[(i - mStartIndex) * 4 + 1] = view.getVolY(this, lastPoint.getSd());
            pts1[(i - mStartIndex) * 4 + 2] = currentPointX;
            pts1[(i - mStartIndex) * 4 + 3] = view.getVolY(this, currentPoint.getSd());

            if (i<1){
                continue;
            }

            ISKDJ lPoint = (ISKDJ) view.getItem(i - 1);

            if (lPoint.getSk() >= lineBottom && lPoint.getSk() <= lineTop) {
                if (currentPoint.getSk() < lineBottom) {
                    // 画下起始竖线
                    canvas.drawLine( currentPointX,view.getVolY(this, lineBottom) , currentPointX, view.getChartRect(this).bottom,mKPaint);

                } else if (currentPoint.getSk() > lineTop) {
                    // 画上终止竖线
                    canvas.drawLine( currentPointX,view.getChartRect(this).top , currentPointX,view.getVolY(this, lineTop) ,mDPaint);
                }
            } else  if (  lPoint.getSk() < lineBottom ){
                if (currentPoint.getSk()  > lineBottom){
                    // 画下终止竖线
                    canvas.drawLine( currentPointX,view.getChartRect(this).bottom , currentPointX,view.getVolY(this, lineBottom) ,mDPaint);
                }
            }else if ( lPoint.getSk()  > lineTop){
                if (currentPoint.getSk()  < lineTop){
                    // 画上起始竖线
                    canvas.drawLine( currentPointX,view.getVolY(this, lineTop) , currentPointX, view.getChartRect(this).top,mKPaint);
                }
            }
        }

        view.drawLines(canvas, mKPaint, pts);
        view.drawLines(canvas, mDPaint, pts1);


        /**
         * 绘制上下标准线
         */
        Rect chartRect = view.getChartRect(this);

        float startX = view.getX(mStartIndex);
        float endX = view.getX(mStopIndex);

        float volY1 = view.getVolY(this, lineTop);
        if (volY1 >= chartRect.top) {
            canvas.drawLine(startX, volY1, endX, volY1, mKPaint);
        }

        float volY = view.getVolY(this, lineBottom);
        if (volY <= chartRect.bottom) {
            canvas.drawLine(startX, volY, endX, volY, mDPaint);
        }

    }

    /**
     * 设置K颜色
     */
    public void setKColor(int color) {
        mKPaint.setColor(color);
    }

    /**
     * 设置D颜色
     */
    public void setDColor(int color) {
        mDPaint.setColor(color);
    }


    /**
     * 设置曲线宽度
     */
    public void setLineWidth(float width) {
        mKPaint.setStrokeWidth(width);
        mDPaint.setStrokeWidth(width);
    }

    /**
     * 设置文字大小
     */
    public void setTextSize(float textSize) {
        mKPaint.setTextSize(textSize);
        mDPaint.setTextSize(textSize);
    }
}
